using System;
using System.Collections.Generic;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using Spread.Common.DataStore.Models;
using Spread.Common.DataStore;
using FarPoint.Win.Spread;
using FarPoint.Win.Spread.Chart;

namespace Spread.Sparkline.Features
{
    public partial class CustomSparkline : Spread.Common.Features.Sample.BaseSampleControl
    {
        private BulletSparklineGroup _sparklineGroup;

        public CustomSparkline()
        {
            InitializeComponent();
            Name = "Custom Sparkline";

            comboOrientation.SelectedIndex = 0;
            btnColor.BackColor = Color.LightSteelBlue;

            fpSpread.ActiveSheet.DataSource = GetDataTable();

            AddSparklineColumn(fpSpread.ActiveSheet);
            SetSheetProperties(fpSpread.ActiveSheet);

            btnChange.Click += BtnChange_Click;
            btnColor.Click += BtnColor_Click;
        }

        private void SetSheetProperties(SheetView sheet)
        {
            sheet.Columns[sheet.ColumnCount - 1].Width = 120;
            sheet.SetMultipleRowHeights(0, sheet.RowCount, 24);
        }

        private void AddSparklineColumn(SheetView sheet)
        {
            sheet.AddColumns(sheet.ColumnCount, 1);
            int col = sheet.ColumnCount - 1;

            _sparklineGroup = new BulletSparklineGroup();
            BulletSparklineSetting setting = new BulletSparklineSetting(10, Color.LightSteelBlue, false);
            _sparklineGroup.Setting = setting;
            _sparklineGroup.Renderer = new BulletSparklineRenderer();

            sheet.SparklineContainer.Add(_sparklineGroup);

            for (int row = 0; row < sheet.RowCount; ++row)
            {
                _sparklineGroup.Add(new Spread.Sparkline.BulletSparkline(sheet, row, col, new SheetCellRange(sheet, row, 1, 1, 1),
                    new SheetCellRange(sheet, row, 2, 1, 1), new SheetCellRange(sheet, row, 3, 1, 1),
                    new SheetCellRange(sheet, row, 5, 1, 1), new SheetCellRange(sheet, row, 4, 1, 1),
                    new SheetCellRange(sheet, row, 6, 1, 1)));
            }
        }

        private DataTable GetDataTable()
        {
            List<Student> students = new DataService().StudentDashboardDataSource.GetAllStudents();
            DataTable table = new DataTable();
            table.Columns.Add("StudentName", typeof(string));
            table.Columns.Add("Score", typeof(int));
            table.Columns.Add("Target", typeof(int));
            table.Columns.Add("Forecast", typeof(int));
            table.Columns.Add("Bad", typeof(int));
            table.Columns.Add("Good", typeof(int));
            table.Columns.Add("Maximum", typeof(int));

            Random random = new Random();
            foreach (Student student in students)
            {
                table.Rows.Add(student.Name, random.Next(30, 100), random.Next(60, 90), random.Next(50, 80), 40, 60,
                    100);
            }
            return table;
        }

        private void BtnColor_Click(object sender, EventArgs e)
        {
            ColorDialog dialog = new ColorDialog();
            if (dialog.ShowDialog() == DialogResult.OK)
                btnColor.BackColor = dialog.Color;
        }

        private void BtnChange_Click(object sender, EventArgs e)
        {
            BulletSparklineSetting setting = new BulletSparklineSetting();
            setting.Vertical = (comboOrientation.SelectedItem.ToString() == "Vertical");
            setting.ColorScheme = btnColor.BackColor;
            setting.TickUnit = (double)numericTickUnit.Value;

            _sparklineGroup.Setting = setting;
        }
    }
}


//
//CustomSparkline.Designer.cs
//


namespace Spread.Sparkline.Features
{
    partial class CustomSparkline
    {
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.fpSpread = new FarPoint.Win.Spread.FpSpread();
            this.fpSpread_Sheet1 = new FarPoint.Win.Spread.SheetView();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.btnChange = new System.Windows.Forms.Button();
            this.label3 = new System.Windows.Forms.Label();
            this.btnColor = new System.Windows.Forms.Button();
            this.label2 = new System.Windows.Forms.Label();
            this.comboOrientation = new System.Windows.Forms.ComboBox();
            this.label1 = new System.Windows.Forms.Label();
            this.numericTickUnit = new System.Windows.Forms.NumericUpDown();
            this.pnlMain.SuspendLayout();
            this.pnlControls.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread_Sheet1)).BeginInit();
            this.groupBox1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.numericTickUnit)).BeginInit();
            this.SuspendLayout();
            // 
            // pnlMain
            // 
            this.pnlMain.Controls.Add(this.fpSpread);
            // 
            // pnlControls
            // 
            this.pnlControls.Controls.Add(this.groupBox1);
            // 
            // fpSpread
            // 
            this.fpSpread.Dock = System.Windows.Forms.DockStyle.Fill;
            this.fpSpread.Location = new System.Drawing.Point(0, 0);
            this.fpSpread.Name = "fpSpread";
            this.fpSpread.Sheets.AddRange(new FarPoint.Win.Spread.SheetView[] {
            this.fpSpread_Sheet1});
            this.fpSpread.Size = new System.Drawing.Size(477, 487);
            this.fpSpread.TabIndex = 0;
            // 
            // fpSpread_Sheet1
            // 
            this.fpSpread_Sheet1.Reset();
            this.fpSpread_Sheet1.SheetName = "Sheet1";
            // Formulas and custom names must be loaded with R1C1 reference style
            this.fpSpread_Sheet1.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.R1C1;
            this.fpSpread_Sheet1.ReferenceStyle = FarPoint.Win.Spread.Model.ReferenceStyle.A1;
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.numericTickUnit);
            this.groupBox1.Controls.Add(this.btnChange);
            this.groupBox1.Controls.Add(this.label3);
            this.groupBox1.Controls.Add(this.btnColor);
            this.groupBox1.Controls.Add(this.label2);
            this.groupBox1.Controls.Add(this.comboOrientation);
            this.groupBox1.Controls.Add(this.label1);
            this.groupBox1.Dock = System.Windows.Forms.DockStyle.Top;
            this.groupBox1.Location = new System.Drawing.Point(0, 0);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(157, 171);
            this.groupBox1.TabIndex = 0;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Sparkline Setting";
            // 
            // btnChange
            // 
            this.btnChange.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.btnChange.Location = new System.Drawing.Point(28, 132);
            this.btnChange.Name = "btnChange";
            this.btnChange.Size = new System.Drawing.Size(104, 23);
            this.btnChange.TabIndex = 6;
            this.btnChange.Text = "Change Settings";
            this.btnChange.UseVisualStyleBackColor = true;
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(7, 99);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(53, 13);
            this.label3.TabIndex = 4;
            this.label3.Text = "Tick Unit:";
            // 
            // btnColor
            // 
            this.btnColor.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.btnColor.Location = new System.Drawing.Point(74, 60);
            this.btnColor.Name = "btnColor";
            this.btnColor.Size = new System.Drawing.Size(77, 23);
            this.btnColor.TabIndex = 3;
            this.btnColor.UseVisualStyleBackColor = true;
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(7, 65);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(34, 13);
            this.label2.TabIndex = 2;
            this.label2.Text = "Color:";
            // 
            // comboOrientation
            // 
            this.comboOrientation.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.comboOrientation.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.comboOrientation.FormattingEnabled = true;
            this.comboOrientation.Items.AddRange(new object[] {
            "Horizontal",
            "Vertical"});
            this.comboOrientation.Location = new System.Drawing.Point(74, 26);
            this.comboOrientation.Name = "comboOrientation";
            this.comboOrientation.Size = new System.Drawing.Size(77, 21);
            this.comboOrientation.TabIndex = 1;
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(7, 29);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(61, 13);
            this.label1.TabIndex = 0;
            this.label1.Text = "Orientation:";
            // 
            // numericTickUnit
            // 
            this.numericTickUnit.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.numericTickUnit.Location = new System.Drawing.Point(74, 97);
            this.numericTickUnit.Maximum = new decimal(new int[] {
            10,
            0,
            0,
            0});
            this.numericTickUnit.Name = "numericTickUnit";
            this.numericTickUnit.Size = new System.Drawing.Size(77, 20);
            this.numericTickUnit.TabIndex = 7;
            this.numericTickUnit.Value = new decimal(new int[] {
            10,
            0,
            0,
            0});
            // 
            // CustomSparkline
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Name = "CustomSparkline";
            this.pnlMain.ResumeLayout(false);
            this.pnlControls.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.fpSpread_Sheet1)).EndInit();
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.numericTickUnit)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private FarPoint.Win.Spread.FpSpread fpSpread;
        private FarPoint.Win.Spread.SheetView fpSpread_Sheet1;
        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Button btnColor;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.ComboBox comboOrientation;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Button btnChange;
        private System.Windows.Forms.NumericUpDown numericTickUnit;
    }
}
